package com.z.system.vo;

import java.util.List;
import java.util.UUID;

import com.z.system.model.Permission;

/**
 * 菜单更新请求VO类，用于接收前端传过来的菜单更新请求
 */
public class MenuRequestVo {

    private String id; // 修改为String类型以兼容前端传来的数值类型id
    private String name;
    private String parentId;
    private String path;
    private String label;
    private String component;
    private String icon;
    private Boolean isEnable;
    private Integer sort;
    private String type;
    private Boolean isMenu;
    private Boolean keepAlive;
    private Boolean isHide;
    private Boolean isHideTab;
    private String link;
    private Boolean isIframe;
    private Boolean showBadge;
    private String showTextBadge;
    private Boolean fixedTab;
    private String activePath;
    private List<String> roles;
    private String authName;
    private String authLabel;
    private String authIcon;
    private Integer authSort;
    private Meta meta;

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取UUID类型的id
     * 处理前端传来的id为"0"或其他非UUID格式的情况
     * 
     * @return UUID类型的id，如果id为"0"或无效则返回null
     */
    public UUID getIdAsUUID() {
        if (id == null || id.isEmpty() || "0".equals(id)) {
            return null;
        }
        try {
            return UUID.fromString(id);
        } catch (IllegalArgumentException e) {
            // 如果id不是有效的UUID格式，返回null表示需要创建新记录
            return null;
        }
    }

    /**
     * 判断是否为新增操作
     * 
     * @return 如果id为null、空或"0"，则返回true
     */
    public boolean isNew() {
        return id == null || id.isEmpty() || "0".equals(id);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getComponent() {
        return component;
    }

    public void setComponent(String component) {
        this.component = component;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Boolean getIsEnable() {
        return isEnable;
    }

    public void setIsEnable(Boolean isEnable) {
        this.isEnable = isEnable;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public Boolean getIsMenu() {
        return isMenu;
    }

    public void setIsMenu(Boolean isMenu) {
        this.isMenu = isMenu;
    }

    public Boolean getKeepAlive() {
        return keepAlive;
    }

    public void setKeepAlive(Boolean keepAlive) {
        this.keepAlive = keepAlive;
    }

    public Boolean getIsHide() {
        return isHide;
    }

    public void setIsHide(Boolean isHide) {
        this.isHide = isHide;
    }

    public Boolean getIsHideTab() {
        return isHideTab;
    }

    public void setIsHideTab(Boolean isHideTab) {
        this.isHideTab = isHideTab;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public Boolean getIsIframe() {
        return isIframe;
    }

    public void setIsIframe(Boolean isIframe) {
        this.isIframe = isIframe;
    }

    public Boolean getShowBadge() {
        return showBadge;
    }

    public void setShowBadge(Boolean showBadge) {
        this.showBadge = showBadge;
    }

    public String getShowTextBadge() {
        return showTextBadge;
    }

    public void setShowTextBadge(String showTextBadge) {
        this.showTextBadge = showTextBadge;
    }

    public Boolean getFixedTab() {
        return fixedTab;
    }

    public void setFixedTab(Boolean fixedTab) {
        this.fixedTab = fixedTab;
    }

    public String getActivePath() {
        return activePath;
    }

    public void setActivePath(String activePath) {
        this.activePath = activePath;
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }

    public String getAuthName() {
        return authName;
    }

    public void setAuthName(String authName) {
        this.authName = authName;
    }

    public String getAuthLabel() {
        return authLabel;
    }

    public void setAuthLabel(String authLabel) {
        this.authLabel = authLabel;
    }

    public String getAuthIcon() {
        return authIcon;
    }

    public void setAuthIcon(String authIcon) {
        this.authIcon = authIcon;
    }

    public Integer getAuthSort() {
        return authSort;
    }

    public void setAuthSort(Integer authSort) {
        this.authSort = authSort;
    }

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    /**
     * Meta内部类，用于存储菜单的元数据
     */
    public static class Meta {
        private String title;
        private String icon;
        private Boolean isHide;
        private Boolean isHideTab;
        private String link;
        private Boolean isIframe;
        private Boolean keepAlive;
        private Boolean showBadge;
        private String showTextBadge;
        private Boolean fixedTab;
        private String activePath;
        private List<String> roles;
        private Boolean isMenu;
        private Integer sort;

        // Getters and Setters
        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public Boolean getIsHide() {
            return isHide;
        }

        public void setIsHide(Boolean isHide) {
            this.isHide = isHide;
        }

        public Boolean getIsHideTab() {
            return isHideTab;
        }

        public void setIsHideTab(Boolean isHideTab) {
            this.isHideTab = isHideTab;
        }

        public String getLink() {
            return link;
        }

        public void setLink(String link) {
            this.link = link;
        }

        public Boolean getIsIframe() {
            return isIframe;
        }

        public void setIsIframe(Boolean isIframe) {
            this.isIframe = isIframe;
        }

        public Boolean getKeepAlive() {
            return keepAlive;
        }

        public void setKeepAlive(Boolean keepAlive) {
            this.keepAlive = keepAlive;
        }

        public Boolean getShowBadge() {
            return showBadge;
        }

        public void setShowBadge(Boolean showBadge) {
            this.showBadge = showBadge;
        }

        public String getShowTextBadge() {
            return showTextBadge;
        }

        public void setShowTextBadge(String showTextBadge) {
            this.showTextBadge = showTextBadge;
        }

        public Boolean getFixedTab() {
            return fixedTab;
        }

        public void setFixedTab(Boolean fixedTab) {
            this.fixedTab = fixedTab;
        }

        public String getActivePath() {
            return activePath;
        }

        public void setActivePath(String activePath) {
            this.activePath = activePath;
        }

        public List<String> getRoles() {
            return roles;
        }

        public void setRoles(List<String> roles) {
            this.roles = roles;
        }

        public Boolean getIsMenu() {
            return isMenu;
        }

        public void setIsMenu(Boolean isMenu) {
            this.isMenu = isMenu;
        }

        public Integer getSort() {
            return sort;
        }

        public void setSort(Integer sort) {
            this.sort = sort;
        }
    }

    public Permission toPermission() {
        Permission permission = new Permission();

        // 设置基本字段
        if (name != null) {
            permission.setName(name);
        }

        // 设置权限标识，使用label字段
        if (label != null) {
            permission.setPermission(label);
        }

        // 特殊处理path字段，避免"/null"值
        if (path != null && !"/null".equals(path)) {
            permission.setHref(path);
        }

        // 设置其他基本字段
        if (component != null) {
            permission.setComponent(component);
        }
        if (icon != null) {
            permission.setIcon(icon);
        }
        if (sort != null) {
            permission.setSort(sort);
        }

        // 处理布尔值转整数的字段
        if (isEnable != null) {
            permission.setEnable(isEnable ? 1 : 0);
        }

        // 处理布尔类型字段
        if (keepAlive != null) {
            permission.setKeepAlive(keepAlive);
        }
        if (isHide != null) {
            permission.setHide(isHide);
        }
        if (isHideTab != null) {
            permission.setHideTab(isHideTab);
        }
        if (link != null) {
            permission.setLink(link);
        }
        if (isIframe != null) {
            permission.setIframe(isIframe);
        }
        if (showBadge != null) {
            permission.setShowBadge(showBadge);
        }
        if (showTextBadge != null) {
            permission.setShowTextBadge(showTextBadge);
        }
        if (fixedTab != null) {
            permission.setFixedTab(fixedTab);
        }
        if (activePath != null) {
            permission.setActivePath(activePath);
        }

        // 处理parentId字段
        if (parentId != null && !parentId.isEmpty()) {
            permission.setParentId(UUID.fromString(parentId));
        }

        // 处理isMenu字段，设置为页面类型
        if (Boolean.TRUE.equals(isMenu)) {
            permission.setType(1); // 设置为页面类型
            if ("0".equals(type)) {
                permission.setType(0);
            }
        } else {
            // 默认设置为按钮类型
            permission.setType(2);
        }

        // 从meta对象中获取更多信息，如果直接字段未设置
        if (meta != null) {
            // 处理meta中的基本信息
            if (meta.getTitle() != null && permission.getName() == null) {
                permission.setName(meta.getTitle());
            }

            // 处理meta中的数字类型字段
            if (meta.getSort() != null && permission.getSort() == null) {
                permission.setSort(meta.getSort());
            }

            // 处理meta中的字符串类型字段
            if (meta.getIcon() != null && permission.getIcon() == null) {
                permission.setIcon(meta.getIcon());
            }
            if (meta.getLink() != null && link == null) {
                permission.setLink(meta.getLink());
            }
            // 处理showTextBadge字符串字段，只有当外层没有设置时才从meta获取
            if (meta.getShowTextBadge() != null && showTextBadge == null) {
                permission.setShowTextBadge(meta.getShowTextBadge());
            }

            // 处理meta中的布尔类型字段，优先使用直接字段，如果没有则从meta中获取
            if (meta.getKeepAlive() != null && keepAlive == null) {
                permission.setKeepAlive(meta.getKeepAlive());
            }
            if (meta.getIsHide() != null && isHide == null) {
                permission.setHide(meta.getIsHide());
            }
            if (meta.getIsHideTab() != null && isHideTab == null) {
                permission.setHideTab(meta.getIsHideTab());
            }
            if (meta.getIsIframe() != null && isIframe == null) {
                permission.setIframe(meta.getIsIframe());
            }
            if (meta.getShowBadge() != null && showBadge == null) {
                permission.setShowBadge(meta.getShowBadge());
            }
            if (meta.getFixedTab() != null && fixedTab == null) {
                permission.setFixedTab(meta.getFixedTab());
            }
            if (meta.getActivePath() != null && activePath == null) {
                permission.setActivePath(meta.getActivePath());
            }

            // 处理meta中的isMenu字段，设置为页面类型
            if (Boolean.TRUE.equals(meta.getIsMenu())) {
                permission.setType(1);
            }
        }

        return permission;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}